﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;

public partial class SuperPong : Game
{
    protected override void Draw(GameTime gameTime)
    {
        this.GraphicsDevice.Clear(Color.Black);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);//, Matrix.CreateScale(Scale/600f));
        particleSpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
        switch (gameState)
        {
            case SuperPongControl.Types.GameState.MAIN_MENU:
                drawMainMenu();
                break;
            case SuperPongControl.Types.GameState.CREDITS:
                drawCredits();
                break;
            case SuperPongControl.Types.GameState.INSTRUCTIONS:
                drawInstructions();
                break;
            case SuperPongControl.Types.GameState.NUM_PLAYER_SCREEN:
                drawNumPlayersScreen();
                break;
            case SuperPongControl.Types.GameState.LIFE_SELECTION_SCREEN:
                drawLifeSelectScreen();
                break;
            case SuperPongControl.Types.GameState.DIFFICULTY_SCREEN:
                drawDifficultyScreen();
                break;
            case SuperPongControl.Types.GameState.GAME_RUNNING:
                drawGameRunning();
                break;
            case SuperPongControl.Types.GameState.GAME_END:
                drawGameEnd();
                break;
        }
        if (gameState != SuperPongControl.Types.GameState.GAME_RUNNING)
        {
            pSystem.Draw(particleSpriteBatch, Scale, Vector2.Zero);
        }
        particleSpriteBatch.End();
        spriteBatch.End();
    }

    void drawGameEnd()
    {
        spriteBatch.Draw(this.MainGameOver, new Vector2(300 * CurrAspect - (MainGameOver.Width / 2), 0), Color.White);
        this.GameEndMenu.Draw();

        if (Players[0].Lives == 0)
        {
            spriteBatch.DrawString(TitleFont, "Wygrana niebieskiego.", new Vector2(300 * CurrAspect - 170, 200), Color.DodgerBlue);
        }
        else
        {
            spriteBatch.DrawString(TitleFont, "Wygrana zielonego.", new Vector2(300 * CurrAspect - 170, 200), Color.Orange);
        }
    }

    void drawMainMenu()
    {
        spriteBatch.Draw(this.MainPong, new Vector2(300 * CurrAspect - (MainPong.Width / 2), 0), Color.White);
        this.MainMenu.Draw();
    }

    void drawInstructions()
    {
        float y = this.GraphicsDevice.Viewport.Height - (((float)gameTime.TotalGameTime.Milliseconds / 1000f + (float)gameTime.TotalGameTime.Seconds + gameTime.TotalGameTime.Minutes * 60f + gameTime.TotalGameTime.Hours * 3600f) - instructionsopentime) * 400f;
        if (y < 100)
        {
            y = 100;
        }
        spriteBatch.Draw(this.MainInstructions, new Vector2(300 * CurrAspect - (MainInstructions.Width / 2), 0), Color.White);
        spriteBatch.Draw(this.InstructionPage, new Vector2(300 * CurrAspect - (InstructionPage.Width / 2), y), Color.White);
        this.BackMenu.Draw();
    }

    void drawCredits()
    {
        //float y = this.GraphicsDevice.Viewport.Height - (((float)gameTime.TotalGameTime.Milliseconds / 1000f + (float)gameTime.TotalGameTime.Seconds + gameTime.TotalGameTime.Minutes * 60f + gameTime.TotalGameTime.Hours * 3600f) - creditopentime) * 100f;
        //if (y < -CreditPage.Height)
        //{
        //    this.creditopentime = (float)gameTime.TotalGameTime.Milliseconds / 1000f + (float)gameTime.TotalGameTime.Seconds + gameTime.TotalGameTime.Minutes * 60f + gameTime.TotalGameTime.Hours * 3600f;
        //}
        //spriteBatch.Draw(this.CreditPage, new Vector2(300*CurrAspect - (CreditPage.Width / 2), y), Color.White);
        //spriteBatch.Draw(this.MainCredits, new Vector2(300 * CurrAspect - (MainCredits.Width / 2), 0), Color.White);
        //this.BackMenu.Draw();

        this.Exit();
    }
    
    void drawNumPlayersScreen()
    {
        spriteBatch.Draw(this.MainGamemode, new Vector2(300 * CurrAspect - (MainGamemode.Width / 2), 0), Color.White);
        NumPlayersMenu.Draw();
        BackMenu.Draw();
    }

    void drawLifeSelectScreen()
    {
        spriteBatch.Draw(this.MainLives, new Vector2(300 * CurrAspect - (MainLives.Width / 2), 0), Color.White);
        LifeSelectMenu.Draw();
        BackMenu.Draw();
    }
    void drawDifficultyScreen()
    {
        spriteBatch.Draw(this.MainDifficulty, new Vector2(300 * CurrAspect - (MainDifficulty.Width / 2), 0), Color.White);
        DifficultyMenu.Draw();
        BackMenu.Draw();
    }

    void drawGameRunning()
    {
        for (int i = 0; i < numPlayers; i++)
        {
            this.Players[i].Render(Vector2.Zero);
        }
        ball.Draw(Vector2.Zero);
        if (Paused)
        {
            spriteBatch.DrawString(TitleFont,
                "Paused. Press P to continue.",
                new Vector2(0.5f * CurrAspect * Scale - TitleFont.MeasureString("Paused. Press P to continue.").X / 2, 0.4f * Scale),
                Color.White);
        }
    }
}
